home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / edit / jed207.lha / src / jed.lha / refs.c < prev    next >
C/C++ Source or Header  |  1993-01-05  |  6KB  |  328 lines

  1.  
  2. /*
  3.  * REFS.C
  4.  * (c) 1992 J.Harper
  5.  */
  6.  
  7. #include "jed.h"
  8. #include "jed_protos.h"
  9.  
  10. #define REF_FILE ".jrefs"
  11. #define TEMP_REF "t:tref"
  12. #define COPY_SIZE 1024
  13.  
  14. Prototype   VOID    initpath    (VOID);
  15. Prototype   VOID    killpath    (VOID);
  16. Prototype   VALUE * cmd_addpath (LONG, VALUE *);
  17. Prototype   VALUE * cmd_rempath (LONG, VALUE *);
  18. Prototype   VALUE * cmd_getref    (LONG, VALUE *);
  19. Local        BOOL    copyref    (STRPTR, LONG, LONG);
  20.  
  21. Local struct List PathList;
  22.  
  23. VOID
  24. initpath(VOID)
  25. {
  26.     NewList(&PathList);
  27. }
  28.  
  29. VOID
  30. killpath(VOID)
  31. {
  32.     struct Node *thispath, *nextpath;
  33.     thispath = PathList.lh_Head;
  34.     while(nextpath = thispath->ln_Succ)
  35.     {
  36.     freestring(thispath->ln_Name);
  37.     FreeVec(thispath);
  38.     thispath = nextpath;
  39.     }
  40. }
  41.  
  42. /*
  43.  * (addpath {`dir-name'})
  44.  */
  45. VALUE *
  46. cmd_addpath(LONG argc, VALUE *argv)
  47. {
  48.     VALUE *res = &RES;
  49.     BOOL rc = TRUE;
  50.     while(rc && (argc >= 1))
  51.     {
  52.     if(TPLATE1(VTF_STRING))
  53.     {
  54.         struct Node *newpath = AllocVec(sizeof(struct Node), MEMF_CLEAR);
  55.         if(newpath)
  56.         {
  57.         STRPTR savedpath = savestring(ARG1.val_Value.String);
  58.         if(savedpath)
  59.         {
  60.             newpath->ln_Name = savedpath;
  61.             AddHead(&PathList, newpath);
  62.             settitlefmt("added path %s", (LONG)savedpath);
  63.         }
  64.         else
  65.         {
  66.             FreeVec(newpath);
  67.             settitle(NoMemMsg);
  68.             rc = FALSE;
  69.         }
  70.         }
  71.         else
  72.         {
  73.         settitle(NoMemMsg);
  74.         rc = FALSE;
  75.         }
  76.     }
  77.     else
  78.         goto abort;
  79.     argc--;
  80.     argv++;
  81.     }
  82.     res->val_Type = VTF_NUMBER;
  83.     res->val_Value.Number = rc;
  84. abort:
  85.     return(res);
  86. }
  87.  
  88. /*
  89.  * (rempath {`dir-name'})
  90.  */
  91. VALUE *
  92. cmd_rempath(LONG argc, VALUE *argv)
  93. {
  94.     VALUE *res = &RES;
  95.     BOOL rc = TRUE;
  96.     while(rc && (argc >= 1))
  97.     {
  98.     if(TPLATE1(VTF_STRING))
  99.     {
  100.         struct Node *path = FindName(&PathList, ARG1.val_Value.String);
  101.         if(path)
  102.         {
  103.         Remove(path);
  104.         freestring(path->ln_Name);
  105.         FreeVec(path);
  106.         settitlefmt("removed path entry %s", (LONG)ARG1.val_Value.String);
  107.         }
  108.         else
  109.         {
  110.         settitlefmt("error: can't find path entry %s", (LONG)ARG1.val_Value.String);
  111.         rc = FALSE;
  112.         }
  113.     }
  114.     else
  115.         goto abort;
  116.     argc--;
  117.     argv++;
  118.     }
  119.     res->val_Type = VTF_NUMBER;
  120.     res->val_Value.Number = rc;
  121. abort:
  122.     return(res);
  123. }
  124.  
  125. /*
  126.  * (getref `ref-name')
  127.  * (getref) -- gets reference for word under cursor.
  128.  */
  129. VALUE *
  130. cmd_getref(LONG argc, VALUE *argv)
  131. {
  132.     VW *vw = CurrVW;
  133.     TX *tx = vw->vw_Tx;
  134.     UBYTE refname[40];
  135.     LONG rc = FALSE;
  136.     struct Node *path;
  137.     BOOL kg = TRUE;
  138.  
  139.     if(ARG1.val_Type == VTF_STRING)
  140.     strncpy(refname, ARG1.val_Value.String, 39);
  141.     else
  142.     {
  143.     LINE *line = tx->tx_Lines + vw->vw_CursorPos.pos_Line;
  144.     STRPTR start = line->ln_Line + vw->vw_CursorPos.pos_Col;
  145.     STRPTR end = start;
  146.     UBYTE c;
  147.     while((isalnum(c = *start--)) || (c == '_'))
  148.     {
  149.         if(start == line->ln_Line - 1)
  150.         {
  151.         start--;
  152.         break;
  153.         }
  154.     }
  155.     start += 2;
  156.     while((isalnum(c = *end++)) || (c == '_'))
  157.     {
  158.         if(!c)
  159.         break;
  160.     }
  161.     end--;
  162.     if(start > end)
  163.     {
  164.         settitle("error: cursor not on word");
  165.         setnumres(FALSE);
  166.         return(&RES);
  167.     }
  168.     memcpy(refname, start, end - start);
  169.     refname[end - start] = 0;
  170.     }
  171.     path = PathList.lh_Head;
  172.     while(kg && (path->ln_Succ))
  173.     {
  174.     BPTR olddir;
  175.     if(olddir = Lock(path->ln_Name, SHARED_LOCK))
  176.     {
  177.         BPTR fh;
  178.         olddir = CurrentDir(olddir);
  179.         if(fh = Open(REF_FILE, MODE_OLDFILE))
  180.         {
  181.         UBYTE line[256];
  182.         settitlefmt("scanning %s for references", (LONG)path->ln_Name);
  183.         while(kg && FGets(fh, line, 256))
  184.         {
  185.             if(line[0] == '@')
  186.             {
  187.             STRPTR thisname = line + 1;
  188.             STRPTR thisfile = line + 1;
  189.  
  190.             while(*thisfile++ != '@')
  191.                 ;
  192.             thisfile[-1] = 0;
  193.  
  194.             if(!strcmp(refname, thisname))
  195.             {
  196.                 STRPTR thispos = thisfile;
  197.                 VW *oldvw = CurrVW;
  198.                 kg = FALSE;
  199.                 while(*thispos++ != '@')
  200.                 ;
  201.                 thispos[-1] = 0;
  202.  
  203.                 if(thispos[0] == '#')
  204.                 {
  205.                 LONG startseek;
  206.                 LONG endseek = 0;
  207.  
  208.                 startseek = strtol(thispos + 1, &thispos, 0);
  209.                 if(thispos[0] == '/')
  210.                     endseek = strtol(thispos + 2, &thispos, 0);
  211.                 if(startseek < endseek)
  212.                 {
  213.                     if(copyref(thisfile, startseek, endseek))
  214.                     {
  215.                     rc = newfile(TEMP_REF);
  216.                     DeleteFile(TEMP_REF);
  217.                     if(rc)
  218.                     {
  219.                         TX *newtx = CurrVW->vw_Tx;
  220.                         freestring(newtx->tx_TitleName);
  221.                         freestring(newtx->tx_FileName);
  222.                         newtx->tx_FileName = savestring("");
  223.                         newtx->tx_TitleName = savestring(refname);
  224.                         stdtitle();
  225.                     }
  226.                     }
  227.                 }
  228.                 else
  229.                 {
  230.                     if(rc = activatefile(thisfile))
  231.                     {
  232.                     moveoffset(startseek);
  233.                     resyncxy();
  234.                     }
  235.                 }
  236.                 }
  237.                 else if(thispos[0] == '^')
  238.                 {
  239.                 LONG startline = strtol(thispos + 1, &thispos, 0);
  240.                 if(rc = activatefile(thisfile))
  241.                 {
  242.                     movelinenum(startline);
  243.                     resyncxy();
  244.                 }
  245.                 }
  246.                 else
  247.                 {
  248.                 STRPTR end = thispos;
  249.                 while(*end++ != '@')
  250.                     ;
  251.                 end[-1] = 0;
  252.                 if(activatefile(thisfile))
  253.                 {
  254.                     rc = findcol0(thispos);
  255.                     resyncxy();
  256.                 }
  257.                 }
  258.                 if(rc)
  259.                 {
  260.                 VW *newvw = CurrVW;
  261.                 CurrVW = oldvw;
  262.                 resettitle();
  263.                 stdtitle();
  264.                 CurrVW = newvw;
  265.                 }
  266.             }
  267.             }
  268.         }
  269.         Close(fh);
  270.         }
  271.         else
  272.         settitlefmt("no .jrefs file in dir %s", (LONG)path->ln_Name);
  273.         olddir = CurrentDir(olddir);
  274.         UnLock(olddir);
  275.     }
  276.     if(kg)
  277.         path = path->ln_Succ;
  278.     }
  279.     if((!rc) && (!path->ln_Succ))
  280.     settitlefmt("can't find reference %s", (LONG)refname);
  281.     setnumres(rc);
  282.     return(&RES);
  283. }
  284.  
  285. Local BOOL
  286. copyref(STRPTR fileName, LONG start, LONG end)
  287. {
  288.     BOOL rc = FALSE;
  289.     STRPTR copybuff = AllocVec(COPY_SIZE, 0L);
  290.     if(copybuff)
  291.     {
  292.     BPTR srcfh = Open(fileName, MODE_OLDFILE);
  293.     if(srcfh)
  294.     {
  295.         BPTR dstfh = Open(TEMP_REF, MODE_NEWFILE);
  296.         if(dstfh)
  297.         {
  298.         if(Seek(srcfh, start, OFFSET_BEGINNING) != -1)
  299.         {
  300.             LONG i = start;
  301.             while((end - i) > COPY_SIZE)
  302.             {
  303.             Read(srcfh, copybuff, COPY_SIZE);
  304.             Write(dstfh, copybuff, COPY_SIZE);
  305.             i += COPY_SIZE;
  306.             }
  307.             Read(srcfh, copybuff, end - i);
  308.             Write(dstfh, copybuff, end - i);
  309.             rc = TRUE;
  310.         }
  311.         else
  312.             settitle("error: ref start offset doesn't exist in file");
  313.         Close(dstfh);
  314.         }
  315.         else
  316.         settitle("error: can't open temporary reference file");
  317.         Close(srcfh);
  318.     }
  319.     else
  320.         settitle("error: can't open reference file");
  321.     FreeVec(copybuff);
  322.     }
  323.     else
  324.     settitle(NoMemMsg);
  325.     return(rc);
  326. }
  327.  
  328.